<?php
declare (strict_types=1);

namespace app\v1\middleware;

use app\data\logic\AdminLoginLogic;
use think\facade\Config;
use think\facade\Log;

class CheckToken
{

    protected AdminLoginLogic $logic;

    public function __construct()
    {
        $this->logic = new AdminLoginLogic();
    }

    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        //
        $uri = $request->server('request_uri');
        $method = $request->server('request_method');
        if ($uri != '/v1//login' && $method != 'POST') {
            $access_token = $request->all('access_token');
            $data['access_token'] = $access_token;
            $res = $this->logic->checkToken($data);
            if (!$res['state']) {
                $code = Config::get('code.' . $res['msg']);
                $msg = \lang($res['msg']);
                return json(['code' => $code, 'msg' => $msg]);
            }
        }

        return $next($request);
    }
}
